<?php

namespace App\Repositories;

use App\Models\DistributionStatistics;
use App\Models\Order;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class DistributionStatisticsRepository
 * @package App\Repositories
 * @version November 27, 2017, 3:06 am CST
 *
 * @method DistributionStatistics findWithoutFail($id, $columns = ['*'])
 * @method DistributionStatistics find($id, $columns = ['*'])
 * @method DistributionStatistics first($columns = ['*'])
*/
class DistributionStatisticsRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'date',
        'type',
        'user_id',
        'pairing_number',
        'verify_number',
        'shipping_number',
        'json'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return DistributionStatistics::class;
    }


    public function generate($user,$day=1,$type=1,$date=null)
    {
        if(!$date){
            $date = date('Y-m-d',time()-3600*12);
        }
        if($type==3){
            $end_date = date('Y-m-01 23:59:59',strtotime($date));
        }else{
            $end_date = date('Y-m-d 23:59:59',strtotime($date));
        }
        if($type==1){
            $start_date = date('Y-m-d H:i:s',strtotime($end_date)-3600*24*$day);
        }elseif($type==2){
            $start_date = date('Y-m-d H:i:s',strtotime($end_date)-3600*24*7);
        }elseif($type==3){
            $start_date = date('Y-m-01 H:i:s',strtotime($end_date)-3600*24*7);
        }
        $pairing_order_ids = Order::where([
            ['pairing_at','<',$end_date],
            ['pairing_at','>=',$start_date],
            ['pairing_id', $user->id]
        ])->pluck('id')->toArray();

        $verify_order_ids = Order::where([
            ['verify_at','<',$end_date],
            ['verify_at','>=',$start_date],
            ['verifier_id', $user->id]
        ])->pluck('id')->toArray();

        $shipping_order_ids = Order::where([
            ['real_shipping_at','<',$end_date],
            ['real_shipping_at','>=',$start_date],
            ['courier_id', $user->id]
        ])->pluck('id')->toArray();

        $shipping_orders = Order::where([
            ['real_shipping_at','<',$end_date],
            ['real_shipping_at','>=',$start_date],
            ['courier_id', $user->id]
        ])->get();

        $shipping_overtime_order_ids = [];

        foreach ($shipping_orders as $shipping_order) {
            if(strtotime($shipping_order->real_shipping_end_at) > strtotime($shipping_order->shipping_end_at)){
                $shipping_overtime_order_ids[] = $shipping_order->id;
            }
        }

        $_data = [
            'type'=>$type,
            'user_id'=>$user->id,
            'date'=>$date,
            'pairing_number'=>count($pairing_order_ids),
            'verify_number'=>count($verify_order_ids),
            'shipping_number'=>count($shipping_order_ids),
            'shipping_overtime_number'=>count($shipping_overtime_order_ids),
            'json'=>json_encode([
                'pairing_order_ids'=>$pairing_order_ids,
                'order_ids'=>array_merge($pairing_order_ids,$verify_order_ids,$shipping_order_ids),
                'verify_order_ids'=>$verify_order_ids,
                'shipping_order_ids'=>$shipping_order_ids,
                'shipping_overtime_order_ids'=>$shipping_overtime_order_ids,
                ],256),
        ];
        $_old = DistributionStatistics::where('type', $_data['type'])->where('user_id', $user->id)->where('date',$_data['date'])->first();
        if($_old){
            $_old->update($_data);
        }else{
            DistributionStatistics::create($_data);
        }
    }
}
